﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hypermarket_Serial_Port
{
    class CancelLastItemTransaction : CashierRegisterTaskManager
    {

        #region Constructor
        public CancelLastItemTransaction()
        {
        }
        #endregion

        #region Cancel Last Item in the transaction
        public override List<string> TaskOperation(List<string> receivedInfoList, Dictionary<string, CashierRegister> cashierDict)
        {
            try
            {
                SerialPortLog.Log("Cancel Last Item Start");
                CashierRegister cashierRegister;
                string machineID = receivedInfoList[Constant.ZERO];
                string type = receivedInfoList[Constant.ONE];
                SerialPortLog.LogOperation("Cancel Last Item: " + machineID);

                if (cashierDict.TryGetValue(machineID, out cashierRegister))
                {
                    return CancelLastItem(cashierRegister, machineID, type);
                }
                else
                {
                    //return Constant.LOGIN_FIRST_MSG + Constant.NEWLINE;
                    List<string> output = new List<string>();
                    output.Add(machineID);
                    output.Add(type);
                    output.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("Cancel Last Item Fail");
                    SerialPortLog.Log("Cancel Last Item Fail");
                    return output;
                }

            }
            catch
            {
                //return Constant.PROGRAM_ERROR + Constant.NEWLINE;
                SerialPortLog.LogOperation("Cancel Last Item Error");
                SerialPortLog.Log("Cancel Last Item Error");
                throw;
            }

        }
        #endregion 


        private List<string> CancelLastItem(CashierRegister cashierRegister, string machineID, string type)
        {
            if (cashierRegister.isTransactionStarted())
            {
                string lastBarcode = cashierRegister.getRequestLastItem();
                Transaction tr = cashierRegister.getTransactionData(lastBarcode);
                if (cashierRegister.CancelLastItem())
                {
                    //return Constant.CANCEL_ALL_TRANSACTIONS_MSG + Constant.NEWLINE;
                    List<string> output = new List<string>();
                    double totalPrice = double.Parse(tr.SellingPrice) * tr.UnitSold;
                    totalPrice = (double)Math.Round(Convert.ToDecimal(totalPrice), 2);
                    output.Add(machineID);
                    output.Add(type);
                    output.Add(Constant.SUCCESS);
                    output.Add(totalPrice.ToString());
                    SerialPortLog.LogOperation("Cancel Last Item Successfully");
                    SerialPortLog.Log("Cancel Last Item Successfully");
                    return output;
                }
                else
                {
                    List<string> output = new List<string>();
                    output.Add(machineID);
                    output.Add(type);
                    output.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("Cancel Last Item Fail");
                    SerialPortLog.Log("Cancel Last Item Fail");
                    return output;
                }
            }
            else
            {
                List<string> output = new List<string>();
                output.Add(machineID);
                output.Add(type);
                output.Add(Constant.FAIL);
                SerialPortLog.LogOperation("Cancel Last Item Fail");
                SerialPortLog.Log("Cancel Last Item Fail");
                return output;
                //return Constant.START_TRANSACTION_FIRST + Constant.NEWLINE;
            }
        }

    }
}
